//
//  PhotoItem.m
//  testKeywordDemo
//
//  Created by mei on 14-7-26.
//  Copyright (c) 2014年 Bluewave. All rights reserved.
//

#import "PhotoItemView.h"
#import "PhotoEntity.h"

@implementation PhotoItemView
{
    UIButton *_deleteBtn;
    UIImageView *_photoImageView;
	UIImage *_contentImage;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code

    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    if (!_photoImageView)
    {
        _photoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(2,
                                                                        2,
                                                                        self.frame.size.width - 4,
                                                                        self.frame.size.height - 4)];
        _photoImageView.backgroundColor = [UIColor clearColor];
		_photoImageView.layer.cornerRadius = 2.0;
		_photoImageView.image = self.photo.image;
        [self addSubview:_photoImageView];
    }
    if (!_deleteBtn)
    {
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _deleteBtn.frame = CGRectMake(self.frame.size.width - 15, 0, 15, 15);
        [_deleteBtn setImage:[UIImage imageNamed:@"delete-circular.png"]
                    forState:UIControlStateNormal];
        _deleteBtn.tag = self.index + 1;
        _deleteBtn.hidden = !self.isEditing;
        [_deleteBtn addTarget:self
                       action:@selector(deletePhotoItem:)
             forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_deleteBtn];
    }
}



- (void)setPhoto:(PhotoEntity *)photo
{
    _photo = photo;
    _photoImageView.image = photo.image;
}



- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIButton *deleteBtn = (UIButton *) [self viewWithTag:self.index + 1];
    if (self.isEditing)
    {
        [self deletePhotoItem:deleteBtn];
    }
}

- (void)setIsEditing:(BOOL)isEditing
{
    _isEditing = isEditing;
    _deleteBtn.hidden = !isEditing;
}

/*
    删除图片
 */
- (void)deletePhotoItem:(UIButton *)sender
{
    if (self.isEditing)
    {
        if ([self.delegate respondsToSelector:@selector(photoItemView:)])
        {
            [self.delegate photoItemView:self];
        }
    }

}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
